package com.kasax.skin.support.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Build;
import android.view.LayoutInflater;

import androidx.core.view.LayoutInflaterCompat;

import com.kasax.skin.support.corel.SkinLayoutFactory;
import com.kasax.skin.support.corel.SkinManager;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class LayoutFactoryCompatHelper {
    private static WeakHashMap<Object, SkinLayoutFactory> mSkinDelegateMap;
    private static SkinLayoutFactory skinLayoutFactory;
    private static LayoutInflater layoutInflater;
    private static Map<Activity, SkinLayoutFactory> factoryMap = new HashMap<>();

    public static void compatInstallLayoutFactory(Activity activity, Typeface skinTypeFace) {
        layoutInflater = LayoutInflater.from(activity);
        skinLayoutFactory = getSkinDelegate(activity, skinTypeFace);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            installLayoutFactoryWithAosP();
        } else {
            installLayoutFactoryWioutAosP();
        }

        // 注册观察者
        SkinManager.getInstance().addObserver(skinLayoutFactory);

        // 添加缓存
        factoryMap.put(activity, skinLayoutFactory);
    }

    private static SkinLayoutFactory getSkinDelegate(Activity context, Typeface skinTypeFace) {
        if (mSkinDelegateMap == null) {
            mSkinDelegateMap = new WeakHashMap<>();
        }
        SkinLayoutFactory skinLayoutFactory = mSkinDelegateMap.get(context);
        if (skinLayoutFactory == null) {
            skinLayoutFactory = SkinLayoutFactory.create(context,skinTypeFace);
            mSkinDelegateMap.put(context, skinLayoutFactory);
        }
        return skinLayoutFactory;
    }

    @SuppressLint("SoonBlockedPrivateApi")
    private static void installLayoutFactoryWioutAosP() {
        try {
            Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
            field.setAccessible(true);
            field.setBoolean(layoutInflater, false);
            layoutInflater.setFactory2(skinLayoutFactory);
        } catch (Exception e) {
            LogUtils.e(LayoutFactoryCompatHelper.class, e);
        }
    }

    private static void installLayoutFactoryWithAosP() {
        Class<LayoutInflaterCompat> compatClass = LayoutInflaterCompat.class;
        Class<LayoutInflater> inflaterClass = LayoutInflater.class;
        try {
            Field sCheckedField = compatClass.getDeclaredField("sCheckedField");
            sCheckedField.setAccessible(true);
            sCheckedField.setBoolean(skinLayoutFactory, false);
            Field mFactory = inflaterClass.getDeclaredField("mFactory");
            mFactory.setAccessible(true);
            Field mFactory2 = inflaterClass.getDeclaredField("mFactory2");
            mFactory2.setAccessible(true);
            mFactory2.set(layoutInflater, skinLayoutFactory);
            mFactory.set(layoutInflater, skinLayoutFactory);
        } catch (Exception e) {
            LogUtils.e(LayoutFactoryCompatHelper.class, e);
        }
    }

    public static void remove(Activity activity) {
        // 删除观察者
        SkinLayoutFactory layoutFactory = factoryMap.get(activity);
        SkinManager.getInstance().deleteObserver(layoutFactory);
    }
}
